import TypeName from '@/layouts/content/FormDialog/TypeName'

/**
 * 插槽类型,插槽名称为prop
 */
const slot = () => {
    return {name: TypeName.slot}
}
/** ********************************输入框类型*****************************************/
/** 文本输入框
 *
 * @param maxlength 文本长度最大值  – number
 * @param minlength 文本长度最小值 – number
 * @param placeholder 占位符 – string
 * @param size 输入框大小 - medium/ small/mini
 */
const input = (maxlength, minlength, placeholder, size) => {
    return {
        name: TypeName.input, type: 'text',
        maxlength,
        minlength,
        size,
        placeholder,
        showWordLimit: maxlength !== undefined
    }
}
/** 数字输入框
 *
 * @param max 最大值 – number
 * @param min 最小值 – number
 * @param stepOrControls 步长或者是否开启步长控制 – number
 * @param precision 精度 – number
 * @param placeholder 占位符 – string
 * @param size 输入框大小 - medium/ small/mini
 */
const inputNumber = (max, min, stepOrControls, precision, placeholder, size) => {
    return {
        name: TypeName.inputNumber,
        max: max === null || max === undefined ? 21474836.47 : max > 21474836.47 ? 21474836.47 : max,
        min: min === null || min === undefined ? -21474836.48 : min < -21474836.48 ? -21474836.48 : min,
        step: typeof stepOrControls !== 'boolean' ? stepOrControls : null,
        controls: typeof stepOrControls === 'boolean' ? stepOrControls : true,
        placeholder,
        precision,
        size
    }
}

/** 密码输入框
 *
 * @param maxlength 文本长度最大值  – number
 * @param minlength 文本长度最小值 – number
 * @param size 输入框大小 - medium/ small/mini
 */
const password = (maxlength, minlength, size) => {
    return {
        name: TypeName.input,
        type: 'password',
        maxlength,
        minlength,
        size,
        showPassword: true
    }
}
/** 文本域
 *
 * @param placeholder 占位符 – string
 * @param maxlength 文本长度最大值  – number
 * @param minlength 文本长度最小值 – number
 * @param rows 行数 – number
 * @param autosize 自动调整高度 - boolean/object:{ minRows: 2, maxRows: 6 }
 * @param showWordLimit 是否显示字数统计 - boolean
 */
const textarea = (placeholder, rows, autosize, maxlength, minlength, showWordLimit) => {
    return {
        name: TypeName.input,
        type: 'textarea',
        placeholder,
        maxlength,
        minlength,
        rows,
        autosize,
        showWordLimit
    }
}

/** 复合型输入框
 *
 * @param placeholder 占位符 – string
 * @param max 最大值 – number
 * @param min 最小值 – number
 * @param unit 单位 - string
 * @param slot 插槽位置 - 'append'/'prepend'
 * @param size 输入框大小 - medium/ small/mini
 *
 */
const composite = (placeholder, max, min, unit, slot, size) => {
    return {
        name: TypeName.input,
        type: 'text',
        placeholder,
        max: max === null || max === undefined ? 21474836.47 : max > 21474836.47 ? 21474836.47 : max,
        min: min === null || min === undefined ? -21474836.48 : min < -21474836.48 ? -21474836.48 : min,
        unit,
        slot: slot || 'append',
        size
    }
}

/** ********************************单选框类型*****************************************/
/**
 * 单选框
 * @param options 选项数组 – Array
 * @param label 选项标签字段名 – string:'label'
 * @param value 选项值字段名 – string:'value'
 */
const radio = (options, label, value) => {
    return {
        name: TypeName.radio,
        options,
        label: label || 'label',
        value: value || 'value'
    }
}
/** ********************************日期时间选择类型********************************************/
/**
 * 日期时间选择器
 * @param width 选择器宽度 - number
 */
const datetime = (width) => {
    return {name: TypeName.datetime, width}
}
/** ********************************选择器********************************************/
/** 选择器
 *
 * @param options 选项数组 – Array
 * @param label 选项标签字段名 – string:'label'
 * @param value 选项值字段名 – string:'value'
 * @param key 唯一标识 - string:value
 */
const select = (options, label, value, key) => {
    const config = {
        name: TypeName.select,
        label: label || 'label',
        value: value || 'value',
        key: key || (value || 'value')
    }
    if (typeof options === 'function') config.remoteMethod = options
    else config.options = options
    return config
}
/** 远程选择器
 *
 * @param remoteMethod 远程方法 – function(keyword: string,result:function(Array))
 * @param label 选项标签字段名 – string:'label'
 * @param value 选项值字段名 – string:'value'
 */
const remoteSelect = (remoteMethod, label, value) => {
    return {
        name: TypeName.select,
        remote: true,
        remoteMethod,
        label: label || 'label',
        value: value || 'value'
    }
}
/** *******************************特殊处理类型******************************************/
/**
 * 金额输入框
 * @param max 最大值 – number:21474836.47
 * @param min 最小值 – number:-21474836.48
 * @param placeholder 占位符 – string
 * @param unit 金额单位 - string
 * @param size 输入框大小 - medium/ small/mini
 */
const money = (max, min, placeholder, unit, size) => {
    return {
        name: TypeName.money,
        max: max === null || max === undefined ? 21474836.47 : max > 21474836.47 ? 21474836.47 : max,
        min: min === null || min === undefined ? -21474836.48 : min < -21474836.48 ? -21474836.48 : min,
        placeholder,
        unit,
        size
    }
}
/**
 * 日期时间范围选择器
 * @param width 选择器宽度 - number
 */
const datetimerange = (width) => {
    return {name: TypeName.datetimerange, width}
}

/**
 * 图片上传
 * @param uploadPath 上传路径 - string
 * @param width 图片宽度 - number
 * @param height 图片高度 - number
 */
const imageUpload = (uploadPath, width, height) => {
    return {
        name: TypeName.imageUpload,
        uploadPath,
        width,
        height
    }
}
/**
 * 计算属性
 */
const computed = (func, ...relyProps) => {
    return {
        name: TypeName.computed,
        func,
        relyProps
    }
}

export default {
    slot,
    input,
    inputNumber,
    money,
    imageUpload,
    select,
    remoteSelect,
    password,
    textarea,
    radio,
    datetime,
    datetimerange,
    composite,
    computed
}
